import AbstractProduct.AbstractProductEV;
import AbstractProduct.AbstractProductORV;
import AbstractProduct.AbstractProductSupercar;
import Factory.FactoryBMW;
import Factory.FactoryBenc;
import Factory.FactoryLamborghini;
import Factory.Interface.IAbstractFactoryCar;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;

public class Main {

    //方法1：直接创建相应的工厂
//    public static void main(String[] args) {
//        AbstractProductSupercar mySupercar;
//        AbstractProductORV myORV;
//        AbstractProductEV myEV;
//
//        IAbstractFactoryCar factory = new FactoryBenc();
//        mySupercar= factory.createProductSupercar();
//        myORV= factory.createProductORV();
//        myEV= factory.createProductEV();
//
//        System.out.println(mySupercar.ShowCarName());
//        System.out.println(myORV.ShowCarName());
//        System.out.println(myEV.ShowCarName());
//
//        System.out.println("-----------------------------------");
//
//
//    }


    //方法二：运用xml的方式顺序读取各个工厂
    public static void main(String[] args) {
        try {
            for (int i = 1; i <= ReadClassCountFromXML(); i++) {
                IAbstractFactoryCar factory;

                // 读取XML中的类名
                String className = ReadClassNameFromXML("factory" + i);
                if (!className.isEmpty()) {
                    Class classReflection = Class.forName(className);
                    Object objectReflection = classReflection.newInstance();

                    factory = (IAbstractFactoryCar) objectReflection;
                    AbstractProductSupercar mySupercar = factory.createProductSupercar();
                    AbstractProductORV myORV= factory.createProductORV();
                    AbstractProductEV myEV= factory.createProductEV();
                    System.out.println(mySupercar.ShowCarName());
                    System.out.println(myORV.ShowCarName());
                    System.out.println(myEV.ShowCarName());
                    System.out.println("-----------------------------------");
                } else {
                    System.out.println("未找到id编号为factory" + i + "的汽车工厂");
                }
            }

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public static String ReadClassNameFromXML(String id) {
        String className = "";
        try {
            // 读取XML文件
            DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = dFactory.newDocumentBuilder();
            Document doc = builder.parse(new File("class_5//src//config.xml"));

            // 获取指定ID的类名节点
            NodeList nl = doc.getElementsByTagName("className");
            for (int i = 0; i < nl.getLength(); i++) {
                Element element = (Element) nl.item(i);
                String carId = element.getAttribute("id");
                if (carId.equals(id)) {
                    className = element.getTextContent();
                    break;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return className;
    }

    //返回xml里有多少辆车
    public static int ReadClassCountFromXML() {
        int classCount=0;
        try {
            // 读取XML文件
            DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = dFactory.newDocumentBuilder();
            Document doc = builder.parse(new File("class_5//src//config.xml"));

            // 获取指定ID的类名节点
            NodeList nl = doc.getElementsByTagName("className");
            classCount=nl.getLength();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return classCount;
    }
}